const express = require('express')
const router = express.Router()
const passport = require('passport')
const Invest = require('../../models/Invest')
const pagination = require('mongoose-sex-page')

router.get('/invest/list/all', passport.authenticate('jwt', { session: false }), (req, res) => {
  let investFields = {}
  if (req.query.name) investFields.name = req.query.name
  if (req.query.number) investFields.number = req.query.number
  if (req.query.telephone) investFields.telephone = req.query.telephone
  if (req.query.models) investFields.models = req.query.models
  if (req.query.createTime) investFields.createTime = req.query.createTime
  if (req.query.cash) investFields.cash = req.query.cash
  if (req.query.status) investFields.status = req.query.status
  // page 当前页
  Invest.find(investFields)
    .then((invest) => {
      if (!invest) {
        return res.status(400).json({ status: 400, message: '获取用户列表失败', data: [] })
      }
      const total = invest.length
      return res.json({ status: 200, message: '请求成功', success: true, data: invest, total })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '获取列表失败' })
    })
})

router.get('/invest/list', passport.authenticate('jwt', { session: false }), (req, res) => {
  let investFields = {}
  if (req.query.name) investFields.name = req.query.name
  if (req.query.number) investFields.number = req.query.number
  if (req.query.telephone) investFields.telephone = req.query.telephone
  if (req.query.models) investFields.models = req.query.models
  if (req.query.createTime) investFields.createTime = req.query.createTime
  if (req.query.cash) investFields.cash = req.query.cash
  if (req.query.status) investFields.status = req.query.status
  // page 当前页
  pagination(Invest)
    .find(investFields)
    .page(req.query.pageNum)
    .size(req.query.pageSize)
    .exec()
    .then((invest) => {
      if (!invest) {
        return res.status(400).json({ status: 400, message: '获取用户列表失败', data: [] })
      }
      const total = invest.length
      return res.json({ status: 200, message: '请求成功', success: true, data: invest, total })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '获取列表失败' })
    })
})

router.post('/invest/add', passport.authenticate('jwt', { session: false }), (req, res) => {
  let investField = {}
  if (req.body.name) investField.name = req.body.name
  if (req.body.number) investField.number = req.body.number
  if (req.body.telephone) investField.telephone = req.body.telephone
  if (req.body.models) investField.models = req.body.models
  if (req.body.cash) investField.cash = req.body.cash
  if (req.body.createTime) investField.createTime = req.body.createTime
  if (req.body.status) investField.status = req.body.status
  new Invest(investField)
    .save()
    .then(() => {
      // invest.save().then(invest => res.json(invest));
      return res.json({ status: 200, success: true, message: '添加成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '添加失败!' })
    })
})

router.post('/invest/edit/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  let investField = {}
  if (req.body.name) investField.name = req.body.name
  if (req.body.number) investField.number = req.body.number
  if (req.body.telephone) investField.telephone = req.body.telephone
  if (req.body.models) investField.models = req.body.models
  if (req.body.cash) investField.cash = req.body.cash
  if (req.body.createTime) investField.createTime = req.body.createTime
  if (req.body.status) investField.status = req.body.status
  Invest.findOneAndUpdate({ _id: req.params.id }, { $set: investField }, { new: true })
    .then(() => {
      // invest.save().then(invest => res.json(invest));
      return res.json({ status: 200, success: true, message: '修改成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '修改失败!' })
    })
})

router.delete('/invest/delete/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  Invest.findOneAndDelete({ _id: req.params.id })
    .then((invest) => {
      // invest.save().then(invest => res.json(invest));
      return res.json({ status: 200, success: true, message: '删除成功' })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, error, message: '删除失败!' })
    })
})

router.get('/invest/detail/:id', passport.authenticate('jwt', { session: false }), (req, res) => {
  Invest.findOne({ _id: req.params.id })
    .then((invest) => {
      return res.json({ status: 200, message: '请求成功', success: true, data: invest })
    })
    .catch((error) => {
      return res.status(400).json({ status: 400, message: ' 获取信息失败', error })
    })
})

module.exports = router
